﻿/*
Copyright (c) 2010 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.IO;
using System.Windows.Forms;
using TeachMe.Application.Properties;
using TeachMe.Application.Themes;

namespace TeachMe.Application.Forms.Options
{
    public partial class Appearance : OptionsControl
    {
        public Appearance()
        {
            InitializeComponent();
        }

        internal override void LoadOptions()
        {
            if (String.IsNullOrEmpty(UserSettings.ThemesDirectory) || !Directory.Exists(UserSettings.ThemesDirectory))
            {
                tbThemesDirectory.Text = PathsProvider.DefaultThemesFolder;
            }
            else
            {
                tbThemesDirectory.Text = UserSettings.ThemesDirectory;
            }
            
            LoadThemes();

            tbEntryOpacity.Value = Root.UserSettings.EntryOpacity.Value;
            tbHorizontalDistance.Value = Root.UserSettings.EntriesHorizontalDistance.Value;
            tbVerticalDistance.Value = Root.UserSettings.EntriesVerticalDistance.Value;
            cbHighlightFromInExample.Checked = Root.UserSettings.HighlightFromInExample.Value;
        }

        internal override void SaveOptions()
        {
            UserSettings.ThemesDirectory = tbThemesDirectory.Text;
            
            if (lbThemes.SelectedItem != null)
            {
                if (lbThemes.SelectedItem is String)
                {
                    Root.UseDefaultTheme();
                    UserSettings.CurrentThemePath = null;
                }
                else
                {
                    Theme theme = lbThemes.SelectedItem as Theme;
                    if (theme == null)
                    {
                        throw new InvalidOperationException();
                    }

                    Root.UseTheme(theme);
                    UserSettings.CurrentThemePath = theme.Path;
                }
            }

            Root.UserSettings.EntryOpacity.Value = tbEntryOpacity.Value;
            Root.UserSettings.EntriesHorizontalDistance.Value = tbHorizontalDistance.Value;
            Root.UserSettings.EntriesVerticalDistance.Value = tbVerticalDistance.Value;
            Root.UserSettings.HighlightFromInExample.Value = cbHighlightFromInExample.Checked;
        }

        private void OnRefreshThemesClick(object sender, EventArgs e)
        {
            LoadThemes();
        }

        private void OnSelectThemesDirectoryClick(object sender, EventArgs e)
        {
            if (Directory.Exists(Root.UserSettings.ThemesDirectory))
            {
                dlgThemesFolder.SelectedPath = Root.UserSettings.ThemesDirectory;
            }

            if (dlgThemesFolder.ShowDialog(this) == DialogResult.OK)
            {
                tbThemesDirectory.Text = dlgThemesFolder.SelectedPath;

                LoadThemes();
            }
        }

        private void LoadThemes()
        {
            String directory = tbThemesDirectory.Text;

            lbThemes.Items.Clear();

            lbThemes.Items.Add(Resources.DefaultThemeName);

            if (Directory.Exists(directory))
            {
                Logging.Debug(GetType(), "Loading themes from directory '{0}'", directory);

                string[] files = Directory.GetFiles(directory, "*.tme", SearchOption.AllDirectories);
                Theme defaultSelection = null;

                foreach (string file in files)
                {
                    Logging.Debug(GetType(), "Loading theme '{0}'", file);

                    Theme theme;

                    try
                    {
                        theme = ThemeLoader.LoadFromArchive(Root.TemporaryContentManager, file);
                    }
                    catch (Exception e)
                    {
                        Logging.Error(GetType(), e, "Theme '{0}' could not be loaded.", file);

                        continue;
                    }

                    if (String.Compare(UserSettings.CurrentThemePath, file, true) == 0)
                    {
                        defaultSelection = theme;
                    }

                    lbThemes.Items.Add(theme);
                }

                if (defaultSelection != null)
                {
                    lbThemes.SelectedItem = defaultSelection;
                }
                else
                {
                    lbThemes.SelectedIndex = 0; // default
                }
            }
        }
    }
}
